/*****************************************************************************
 * Copyright (c) 2024, Nations Technologies Inc.
 *
 * All rights reserved.
 * ****************************************************************************
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Nations' name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ****************************************************************************/

/**
 * @file nss32lbx_qflash.h
 * @author Nations Firmware Team
 * @version v1.0.4
 *
 * @copyright Copyright (c) 2024, Nations Technologies Inc. All rights reserved.
 */
#ifndef __NSS32LBX_QFLASH_H__
#define __NSS32LBX_QFLASH_H__

#ifdef __cplusplus
extern "C" {
#endif

#include "nss32lbx.h"
/** @addtogroup NSS32LBX_StdPeriph_Driver
 * @{
 */

/** @addtogroup QFLASH
 * @{
 */

/** @addtogroup QFLASH_Defines
 * @{
 */
#define Qflash_Erase_Sector_Raw     Qflash_Erase_Sector
#define Qflash_Write_Raw            Qflash_Write

#define FLASH_PAGE_SIZE             0x100
#define FLASH_SECTOR_SIZE           0x1000

#ifdef NSS32LB2
#define FLASH_SIZE                  0x200000
#define OTP_SECTOR_SIZE             0x100
#else // NSS32LB1P
#define FLASH_SIZE                  0x80000
#define OTP_SECTOR_SIZE             0x200
#endif

#define ADDRESS_CHECK(address)      (address<FLASH_SIZE)

#define OTP_ADDRESS_SECTOR1         0x1000
#define OTP_ADDRESS_SECTOR2         0x2000
#define OTP_ADDRESS_SECTOR3         0x3000
#define OTP_ADDRESS_TO_SECTOR(addr) (addr & 0x7000)
#define OTP_ADDRESS_CHECK(address) (((address>=OTP_ADDRESS_SECTOR1)&&(address<(OTP_ADDRESS_SECTOR1+OTP_SECTOR_SIZE)))||\
                                    ((address>=OTP_ADDRESS_SECTOR2)&&(address<(OTP_ADDRESS_SECTOR2+OTP_SECTOR_SIZE)))||\
                                    ((address>=OTP_ADDRESS_SECTOR3)&&(address<(OTP_ADDRESS_SECTOR3+OTP_SECTOR_SIZE))))

/**
 * @}
 */

/** @addtogroup QFLASH_ReturnMsg
 * @{
 */
typedef enum{
    FlashOperationSuccess,
    FlashWriteRegFailed,
    FlashTimeOut,
    FlashIsBusy,
    FlashQuadNotEnable,
    FlashAddressInvalid,
}ReturnMsg;
/**
 * @}
 */

/** @addtogroup RCC_Exported_Functions
 * @{
 */
void Qflash_Init(void);
uint32_t Qflash_Erase_Sector(uint32_t address);
uint32_t Qflash_Write(uint32_t address, uint8_t* p_data, uint32_t len);
uint32_t Qflash_Read(uint32_t address, uint8_t* p_data, uint32_t len);

#ifdef USE_OTP_TRIM
uint32_t Qflash_RUID(uint8_t *Identification);

uint32_t OTPTrim_Read(uint32_t addr, uint8_t* p_data, uint32_t len);
uint32_t OTPTrim_Erase(uint32_t addr);
uint32_t OTPTrim_Write(uint32_t addr, uint8_t* p_data, uint32_t len);
uint32_t OTPTrim_Lock(uint32_t addr);
uint32_t OTPTrim_Query(uint32_t addr, uint8_t *status);

uint32_t Qflash_Chip_Erase(void);
#endif

#ifdef __cplusplus
}
#endif

#endif /* __NSS32LBX_QFLASH_H__ */
/**
 * @}
 */

/**
 * @}
 */

/**
 * @}
 */
